package trystero.lib.reader;

import trystero.lib.http.HTTPRequestState;
import trystero.lib.http.POSTData;
import trystero.lib.auth.ReaderAuth;
import java.util.Vector;

public class ReaderRequestStates
{
   private static String base = "http://www.google.com/reader/api/0/";

   /*
    * Stream/Contents
    *
    * Request type: GET
    * Returns: JSON (StreamContents)
    * Sub-URL arguments: <tag>
    * Arguments:
    *    sharers=?
    *    n=<integer>
    *    ck=<time>
    */    

   // As is, equivalent to using a tag of state/-/com.google/reading-list
   public static HTTPRequestState streamContents()
   {
      HTTPRequestState state = new HTTPRequestState();
      state.url(base + "stream/contents/");
      state.data(null);

      return state;
   }

   public static HTTPRequestState streamContents(String tag)
   {
      HTTPRequestState state = new HTTPRequestState();
      state.url(base + "stream/contents/" + tag);
      state.data(null);

      return state;
   }

   public static HTTPRequestState streamContents(int count)
   {
      HTTPRequestState state = new HTTPRequestState();
      state.url(base + "stream/contents/" 
	 + "?n=" + count);
      state.data(null);

      return state;
   }

   public static HTTPRequestState streamContents(String tag, int count)
   {
      HTTPRequestState state = new HTTPRequestState();
      state.url(base + "stream/contents/" + tag 
	 + "?n=" + count);
      state.data(null);

      return state;
   }

   /*
    * Stream/Items/Ids
    *
    * Request type: GET
    * Returns: JSON <StreamIds>
    * Required:
    *   n=1000
    *   s=<tag> (feed or splice?)
    *   output=json
    * Arguments:
    *   r=<sort> (a|o|d)
    *   (xt=<tag>)
    */
   public static HTTPRequestState streamItemsIds(String label, int count)
   {
      HTTPRequestState state = new HTTPRequestState();
      state.url(base + "stream/items/ids" 
	 + "?output=json" 
	 + "&s=" + label 
	 + "&n=" + count);
      state.data(null);

      return state;
   }

   /* Stream/Items/Contents
    * Request type: POST
    * Arguments:
    *    sharers=??
    *    ck=<time>
    *    freshness=<boolean> (??)
    * Data:
    *    (i=<long-id>&)*
    *    T=token
    */
   public static HTTPRequestState streamItemsContents(Vector ids)
   {
      HTTPRequestState state = new HTTPRequestState();
      state.url(base + "stream/items/contents/");

      POSTData data = new POSTData();

      for(int i = 0; i < ids.size(); i++)
	 data.add("i", (String)ids.get(i));

      state.data(data);

      return state;
   }

   /*
    * Stream/Items/Count
    * 
    * Request type: GET
    * Returns: <integer>
    * Required:
    *   s=<tag>
    * Arguments:
    *   ck=<time>
    */
   public static HTTPRequestState streamItemsCount(String label)
   {
      HTTPRequestState state = new HTTPRequestState();
      state.url(base + "stream/items/count" 
	 + "?s=" + label);
      state.data(null);

      return state;
   }

   /*
    * Search/Items/Ids
    *
    * Request type: GET
    * Returns: JSON <SearchIds>
    * Required:
    *    q=<string> (query)
    * Arguments:
    *    num=1000
    *    output=json
    *    ck=<time>
    */
   public static HTTPRequestState search(String query)
   {
      HTTPRequestState state = new HTTPRequestState();
      state.url(base + "search/items/ids" 
	 + "?output=json" 
	 + "&q=" + query);
      state.data(null);

      return state;
   }

   public static HTTPRequestState search(String query, int count)
   {
      HTTPRequestState state = new HTTPRequestState();
      state.url(base + "stream/items/ids" 
	 + "?q=" + query 
	 + "&n=" + count);
      state.data(null);

      return state;
   }

   /*
    * Subscription/List
    *
    * Request type: GET
    * Returns: JSON <SubscriptionList>
    * Required:
    *    output=json
    */
   public static HTTPRequestState subscriptionList()
   {
      HTTPRequestState state = new HTTPRequestState();
      state.url(base + "subscription/list" 
	 + "?output=json");
      state.data(null);

      return state;
   }

   /*
    * Tag/List
    *
    * Request type: GET
    * Returns: JSON <TagList>
    * Required:
    *   output=json
    */
   public static HTTPRequestState tagList()
   {
      HTTPRequestState state = new HTTPRequestState();
      state.url(base + "tag/list" 
	 + "?output=json");
      state.data(null);

      return state;
   }

   /*
    * Edit-Tag
    *
    * Request type: POST
    * Returns: OK
    * Data:
    *    either 
    *       a=<tag>
    *       r=<tag>
    *    async=<boolean> ??
    *    s=feed/...
    *    pos=<int> ??
    *    i=<id> (tag/...)
    *    T=<token>
    */
   public static HTTPRequestState editTag(String id, boolean isAdd, String tag) throws Exception 
   {
      HTTPRequestState state = new HTTPRequestState();
      state.url(base + "edit-tag");

      POSTData data = new POSTData();

      //data.add("s", feed);
      data.add("i", id);

      String action;
      if(isAdd == true)
	 action = "a";
      else
	 action = "r";

      data.add(action, tag);

      state.data(data);

      return state;
   }

   public static HTTPRequestState editTag(Vector ids, boolean isAdd, String tag) throws Exception
   {
      HTTPRequestState state = new HTTPRequestState();
      state.url(base + "edit-tag");

      POSTData data = new POSTData();

      for(int i = 0; i < ids.size(); i++)
         data.add("i", (String)ids.get(i));

      String action;
      if(isAdd == true)
	 action = "a";
      else
	 action = "r";
      data.add(action, tag);

      state.data(data);

      return state;
   }

   /* 
    * Unread-count
    *
    * Request type: GET
    * Returns: JSON 
    * Required:
    *    output=json
    * Arguments
    *    allcomments=true|false
    */
   public static HTTPRequestState unreadCount()
   {
      HTTPRequestState state = new HTTPRequestState();
      state.url(base + "unread-count" 
	 + "?output=json");
      state.data(null);

      return state;
   }

   /*
    * Preference/Set
    * 
    * Request type: POST
    * Returns: OK
    * Required:
    *    k=<key>
    *    v=<value>
    *    T=<token>
    */
   public static HTTPRequestState preferenceSet(String key, String value)
   {
      HTTPRequestState state = new HTTPRequestState();
      state.url(base + "preference/set");

      POSTData data = new POSTData();
      data.add("k", key);
      data.add("v", value);
      state.data(data);

      return state;
   }

   /*
    * Preference/Stream/List
    *
    * Request type: GET
    * Returns: JSON
    * Required:
    *    output=json
    * Type:
    */
   public static HTTPRequestState preferenceStreamList()
   {
      HTTPRequestState state = new HTTPRequestState();
      state.url(base + "preference/stream/list" 
	 + "?output=json");
      state.data(null);

      return state;
   }

   /*
    * Preference/List
    *
    * Request type: GET
    * Returns: JSON
    * Required:
    *    output=json
    * Type:
    */
   public static HTTPRequestState preferenceList()
   {
      HTTPRequestState state = new HTTPRequestState();
      state.url(base + "preference/list" 
	 + "?output=json");
      state.data(null);

      return state;
   }

   /*
    * Friend/Groups
    *
    * Request type: GET
    * Returns: JSON
    * Required:
    *     output=json
    * Type:
    *     sharingGroups
    *        Array:
    *           Object:
    *              isPublic
    *              groupId
    *              isReadOnly
    *              name
    *              isPredefined
    *              isSharing
    */
   public static HTTPRequestState friendGroups()
   { 
      HTTPRequestState state = new HTTPRequestState();
      state.url(base + "friend/groups" 
	 + "?output=json");
      state.data(null);

      return state;
   }

   /*
    * Friend/List
    *
    * Request type: GET
    * Returns: JSON
    * Required:
    *     output=json
    * Arguments:
    *     lookup=FOLLOWING|FOLLOWERS
    * Type:
    *     Object:
    *        friends:
    *           Array:
    *              Object:
    *                 userIds:
    *                    Array (of strings)
    *                 profileIds:
    *                    Array (of strings)
    *                 contactId
    *                 emailAddresses:
    *                    Array (of strings)
    *                 stream
    *                 flags
    *                 types:
    *                    Array (of ints)
    *                 displayName
    *                 givenName
    *                 nickName
    *                 profileCardParams
    *                 groupId:
    *                    Array (of ints)
    *                 websites:
    *                    Array
    */
   public static HTTPRequestState friendList()
   { 
      HTTPRequestState state = new HTTPRequestState();
      state.url(base + "friend/list" + "?output=json");
      state.data(null);

      return state;
   }

   /*
    * Related/List
    *
    * Request type: GET
    * Returns: JSON
    * Required:
    *     output=json
    * Arguments:
    *     n=8
    *     s=<tag>
    * Type:
    *   Object: RelatedList
    *      recs
    *         Array:
    *            Object:
    *               title
    *               snippet
    *               streamId
    *               impressionTime
    */
   public static HTTPRequestState relatedList(String tag)
   {
      HTTPRequestState state = new HTTPRequestState();
      state.url(base + "related/list" 
	 + "?output=json"
	 + "&s=" + tag);
      state.data(null);

      return state;
   }

   public static HTTPRequestState relatedList(String tag, int count)
   { 
      HTTPRequestState state = new HTTPRequestState();
      state.url(base + "related/list" 
	 + "?output=json"
	 + "&n=" + count
	 + "&s=" + tag);
      state.data(null);

      return state;
   }

   /*
    * User-info
    *
    * Request type: GET
    * Returns: JSON
    * Type:
    *    Object:
    *       userId
    *       userName
    *       userProfileId
    *       userEmail
    *       isBloggerUser
    *       signupTimeSec
    */
   public static HTTPRequestState userInfo()
   {
      HTTPRequestState state = new HTTPRequestState();
      state.url(base + "user-info");
      state.data(null);

      return state;
   }

   /*
    * Stream/Details
    * 
    * Request type: GET
    * Returns: JSON 
    * Mandatory:
    *    s=<label> or <feed>
    *    output=json
    * Arguments:
    *    tz=600
    *    fetchTrends=true
    *    ck=<time>
    * Type:
    *    Object:
    *       subscribers
    *       velocity
    *       successfulCrawlTimeUsec
    *       failedCrawlTimeUsec
    *       lastFailureWasParseFAilure
    *       trendsCharts 
    *          Object:
    *             hourChart
    *                Object:
    *                   chartValues
    *                      Array:
    *                         Object:
    *                            key
    *                            value
    *                   chartUrl
    *                   chartJsonUrl
    *                   chartWidth
    *                   chartHeight
    *             dayChart
    *                Object:
    *                   chartValues
    *                      Array:
    *                         Object:
    *                            key
    *                            value
    *                   chartUrl
    *                   chartJsonUrl
    *                   chartWidth
    *                   chartHeight
    *             dowChart
    *                Object:
    *                   chartValues
    *                      Array:
    *                         Object:
    *                            key
    *                            value
    *                   chartUrl
    *                   chartJsonUrl
    *                   chartWidth
    *                   chartHeight
    *             feedUrl
    */
   public static HTTPRequestState streamDetails(String tag)
   {
      HTTPRequestState state = new HTTPRequestState();
      state.url(base + "stream/details" 
	  + "?output=json" 
	  + "&s=" + tag);
      state.data(null);
 
      return state;
   }
}
